//
//  ByDottedLineView.m
//  beibei_ios
//
//  Created by mc on 16/6/20.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByDottedLineView.h"

@implementation ByDottedLineView

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    self.backgroundColor = [UIColor clearColor];
    _direction = DottedLineViewDirectionAutomatic;
    _phase = 0;
    _lineColor = [UIColor blackColor];
    _lengthPattern = @[@2, @2];
}

#pragma mark -

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    DottedLineViewDirection direction = self.direction;
    BOOL isVertical = (self.direction == DottedLineViewDirectionVerticalFromTop || self.direction == DottedLineViewDirectionVerticalFromBottom);
    if (direction == DottedLineViewDirectionAutomatic) {
        isVertical = (rect.size.height > rect.size.width);
        direction = (isVertical ? DottedLineViewDirectionVerticalFromTop : DottedLineViewDirectionHorizontalFromLeft);
    }
    
    CGFloat borderWidth = (isVertical ? rect.size.width : rect.size.height) * [UIScreen mainScreen].scale;
    CGPoint startPoint;
    CGPoint endPoint;
    switch (direction) {
        case DottedLineViewDirectionHorizontalFromLeft:
            startPoint = CGPointMake(rect.origin.x, rect.origin.y);
            endPoint = CGPointMake(CGRectGetMaxX(rect), rect.origin.y);
            break;
        case DottedLineViewDirectionHorizontalFromRight:
            startPoint = CGPointMake(CGRectGetMaxX(rect), rect.origin.y);
            endPoint = CGPointMake(rect.origin.x, rect.origin.y);
            break;
        case DottedLineViewDirectionVerticalFromTop:
            startPoint = CGPointMake(rect.origin.x, rect.origin.y);
            endPoint = CGPointMake(rect.origin.x, CGRectGetMaxY(rect));
            break;
        case DottedLineViewDirectionVerticalFromBottom:
            startPoint = CGPointMake(rect.origin.x, CGRectGetMaxY(rect));
            endPoint = CGPointMake(rect.origin.x, rect.origin.y);
            break;
            
        default:
            startPoint = CGPointMake(rect.origin.x, rect.origin.y);
            endPoint = CGPointMake(rect.origin.x, rect.origin.y);
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineWidth(context, borderWidth);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
    
    int count = (int)[self.lengthPattern count];
    CGFloat cArrayLengthPattern[count];
    for (int i = 0; i < count; ++i) {
        cArrayLengthPattern[i] = (CGFloat)[self.lengthPattern[i] floatValue];
    }
    
    CGContextSetLineDash(context, self.phase, cArrayLengthPattern, count);
    CGContextMoveToPoint(context, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
    CGContextStrokePath(context);
}


@end




@implementation ByDottedLineRectangularView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        ByDottedLineView *line1 = [[ByDottedLineView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 1)];
        line1.direction = DottedLineViewDirectionHorizontalFromRight;
        line1.lineColor = [UIColor brownColor];
        line1.lengthPattern = @[@2, @1];
        [self addSubview:line1];
        
        
        ByDottedLineView *line2 = [[ByDottedLineView alloc] initWithFrame:CGRectMake(0, 1, 1, frame.size.height -2)];
        line2.lengthPattern = @[@2, @1];
        line2 .phase = 100;
        line2 .lineColor = [UIColor brownColor];
        line2 .direction = DottedLineViewDirectionVerticalFromBottom;
        [self addSubview:line2 ];

        
        
        ByDottedLineView *line3 = [[ByDottedLineView alloc] initWithFrame:CGRectMake(0, frame.size.height - 1, frame.size.width, 1)];
        line3.direction = DottedLineViewDirectionHorizontalFromRight;
        line3.lineColor = [UIColor brownColor];
        line3.lengthPattern = @[@2, @1];
        [self addSubview:line3];

        
        
        ByDottedLineView *line4 = [[ByDottedLineView alloc] initWithFrame:CGRectMake(frame.size.width - 1, 1, 1, frame.size.height -2)];
        line4.lengthPattern = @[@2, @1];
        line4 .phase = 100;
        line4 .lineColor = [UIColor brownColor];
        line4 .direction = DottedLineViewDirectionVerticalFromBottom;
        [self addSubview:line4 ];

        
    }
    return self;
}


@end



@implementation ByDottedLineCircleRectangularLabel
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    _phase = 0.0;
    _lineColor = [UIColor lightGrayColor];
    _lengthPattern = @[@2, @1];
    _borderWidth = 1.0;
}

#pragma mark -
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    int count = (int)[_lengthPattern count];
    CGFloat cArrayLengthPattern[count];
    for (int i = 0; i < count; ++i) {
        cArrayLengthPattern[i] = (CGFloat)[_lengthPattern[i] floatValue];
    }
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineDash(context, 0.0, cArrayLengthPattern, count);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.5, 0.5, rect.size.width - 1, rect.size.height - 1) cornerRadius:rect.size.height / 4];
    CGContextAddPath(context, path.CGPath);
    [_lineColor setStroke];
    CGContextDrawPath(context, kCGPathStroke);
}
@end

@implementation ByDottedLineBorderView
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    self.backgroundColor = [UIColor clearColor];
    _phase = 0.0;
    _lineColor = [UIColor blackColor];
    _lengthPattern = @[@2, @2];
    _borderWidth = 1.0;
}

#pragma mark -
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineWidth(context, self.borderWidth * [UIScreen mainScreen].scale);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
    
    int count = (int)[self.lengthPattern count];
    CGFloat cArrayLengthPattern[count];
    for (int i = 0; i < count; ++i) {
        cArrayLengthPattern[i] = (CGFloat)[self.lengthPattern[i] floatValue];
    }
    
    CGContextSetLineDash(context, self.phase, cArrayLengthPattern, count);
    CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
    CGContextAddLineToPoint(context, CGRectGetMaxX(rect), rect.origin.y);
    CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
    CGContextAddLineToPoint(context, rect.origin.x, CGRectGetMaxY(rect));
    CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y);
    CGContextStrokePath(context);
}

@end



@implementation ByDottedLineBorderLable

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    self.backgroundColor = [UIColor clearColor];
    _phase = 0.0;
    _lineColor = [UIColor blackColor];
    _lengthPattern = @[@2, @2];
    _borderWidth = 1.0;
}

#pragma mark -
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineWidth(context, self.borderWidth * [UIScreen mainScreen].scale);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
    
    int count = (int)[self.lengthPattern count];
    CGFloat cArrayLengthPattern[count];
    for (int i = 0; i < count; ++i) {
        cArrayLengthPattern[i] = (CGFloat)[self.lengthPattern[i] floatValue];
    }
    
    CGContextSetLineDash(context, self.phase, cArrayLengthPattern, count);
    CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
    CGContextAddLineToPoint(context, CGRectGetMaxX(rect), rect.origin.y);
    CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
    CGContextAddLineToPoint(context, rect.origin.x, CGRectGetMaxY(rect));
    CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y);
    CGContextStrokePath(context);
}

@end



@implementation ByDottedLineBorderButton


- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    self.backgroundColor = [UIColor clearColor];
    _phase = 0.0;
    _lineColor = [UIColor blackColor];
    _lengthPattern = @[@2, @2];
    _borderWidth = 1.0;
}

#pragma mark -
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineWidth(context, self.borderWidth * [UIScreen mainScreen].scale);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
    
    int count = (int)[self.lengthPattern count];
    CGFloat cArrayLengthPattern[count];
    for (int i = 0; i < count; ++i) {
        cArrayLengthPattern[i] = (CGFloat)[self.lengthPattern[i] floatValue];
    }
    
    CGContextSetLineDash(context, self.phase, cArrayLengthPattern, count);
    CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
    CGContextAddLineToPoint(context, CGRectGetMaxX(rect), rect.origin.y);
    CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
    CGContextAddLineToPoint(context, rect.origin.x, CGRectGetMaxY(rect));
    CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y);
    CGContextStrokePath(context);
}

@end



@implementation ByDottedLineSecondBorderButton
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    self.backgroundColor = [UIColor clearColor];
    _phase = 0.0;
    _lineColor = BY_THEME_COLOR;
    _lengthPattern = @[@5, @2];
    _borderWidth = 1.0;
}

#pragma mark -
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    int count = (int)[_lengthPattern count];
    CGFloat cArrayLengthPattern[count];
    for (int i = 0; i < count; ++i) {
        cArrayLengthPattern[i] = (CGFloat)[_lengthPattern[i] floatValue];
    }
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineDash(context, 0.0, cArrayLengthPattern, count);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.5, 0.5, rect.size.width - 1, rect.size.height - 1) cornerRadius:3];
    CGContextAddPath(context, path.CGPath);
    [_lineColor set];
    CGContextStrokePath(context);
}
@end
